<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="600px">
		<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col :span="24">
					<el-form-item label="提现金额" prop="apply_money">
						<el-input v-model="form.apply_money" disabled />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col :span="24">
					<el-form-item label="需打款金额" prop="need_remit_money">
						<el-input v-model="form.need_remit_money" disabled />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col :span="24">
					<el-form-item label="审核结果" prop="status">
						<el-radio-group v-model="form.status">
							<el-radio label="1">同意打款</el-radio>
							<el-radio label="2">拒绝打款</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-if="form.status == '1'" :gutter="10" class="mb20">
				<el-col :span="24">
					<el-form-item label="确认密码" prop="password">
						<el-input v-model.number="form.password" placeholder="请输入打款密码(123456)" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-if="form.status == '2'" :gutter="10" class="mb20">
				<el-col :span="24">
					<el-form-item label="拒绝原因" prop="cancel_reason">
						<el-input v-model="form.cancel_reason" placeholder="请输入拒绝原因" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm">确定</el-button>
				<el-button @click="onCancel">取消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
import { ElMessage } from 'element-plus';
import { withdrawalPaymentBatch } from '/@/api/admin1';

export default {
	name: 'distributorWithdrawalAuditDialog',
	setup() {
		const state: any = reactive({
			title: '分销商提现审核',
			open: false,
			form: {},
			rules: {
				password: [{ required: true, message: '确认密码不能为空', trigger: 'blur' }],
				cancel_reason: [{ required: true, message: '拒绝原因不能为空', trigger: 'blur' }],
			}, // 表单校验
			submitLoading: false,
		});

		const formRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					console.log('state.form', state.form);
					_withdrawalPaymentBatch(state.form);
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			reset();
			callback = fn;
			state.open = true;
			state.form = {
				...config,
				password: '',
				status: '1',
				cancel_reason: '',
			};
		};
		// 表单重置
		const reset = () => {
			state.form = {
				apply_money: '',
				need_remit_money: '',
				withdraw_ids: '',
				password: '',
				status: '1',
				cancel_reason: '',
			};
			(formRef.value as any)?.resetFields();
		};

		/********** 网络事件 **********/
		// 审核-打款
		const _withdrawalPaymentBatch = (_data: object) => {
			withdrawalPaymentBatch(_data)
				.then((res: any) => {
					console.log('审核-打款', res);
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch(() => {
					state.submitLoading = false;
				});
		};

		return {
			formRef,
			onSubmitForm,
			openDialog,
			onCancel,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
